﻿using System;
using System.Collections.Generic;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Goalchee.Common;
using Goalchee.DataModel;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Goalchee
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class TagsGoal : LayoutAwarePage
    {
        private readonly DataServiceCall _dataService = new DataServiceCall ( );
        private List<GoalData> _goalDataList;
        private string _queryText;

        public TagsGoal ( )
        {
            this.InitializeComponent ( );
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState ( Object navigationParameter, Dictionary<String, Object> pageState )
        {
            _queryText = navigationParameter as String;

            if ( _queryText == null )
            {
                this.Frame.Navigate ( typeof ( GoalsPerson ), navigationParameter );
            }
            else
            {
                var sampleDataGroups = _dataService.GetSearchGoalsForTag ( _queryText );
                _goalDataList = _dataService.ReadSearchGoalsFromTags ( sampleDataGroups );
                RenderPage ( _goalDataList );
            }
        }

        private void RenderPage ( List<GoalData> readSearchGoalsFromTags )
        {
            pageSubTitle.Text = _queryText.ToUpper ( );
            string headertext = string.Format ( "total match {0}. few goals matching the selected tag",
                                            _dataService.TotalGoals );

            goalHeader.Text = headertext;
            int count = 0;
            Random r = new Random ( 5 );

            foreach ( GoalData goalItem in readSearchGoalsFromTags )
            {
                count++;
                Button hlButton = new Button
                {
                    Name = goalItem.Name + count,
                    //NavigateUri = new Uri ( string.Format ( "{0}{1}?q={2}&api_key={3}", ThingsUrl, SearchGoals, readPopularTag.TagName, Apikey ), UriKind.Absolute ),
                    Content = goalItem.Name,
                    FontSize = r.Next ( 10, 20 ),
                    Foreground = new SolidColorBrush ( Colors.Black ),
                    BorderThickness = new Thickness ( 5, 5, 5, 5 ),

                    Width = 150,
                    Height = 50,
                    CommandParameter = goalItem.GoalId
                };
                hlButton.Click += hlButton_Click;
                itemsOne.Items.Add ( hlButton );
            }
        }

        private void hlButton_Click ( object sender, RoutedEventArgs e )
        {
            string goalid = ( ( Windows.UI.Xaml.Controls.Primitives.ButtonBase ) ( sender ) ).CommandParameter.ToString ( );
            GoalData passThisGoal = _goalDataList.Find ( s => s.GoalId == goalid );
            LoadState ( passThisGoal, null );
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState ( Dictionary<String, Object> pageState )
        {
        }
    }
}
